package com.fw.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fw.model.Permission;
import com.fw.service.PermissionService;
import com.fw.service.impl.PermissionServiceImpl;
import com.jfinal.core.Controller;

public class PermissionController extends Controller{
	PermissionService permissonService = null;
	public void toPermissonListView(){
		renderJsp("/WEB-INF/jsp/permisson_list.jsp");
	}
	public void query_allPermissonByPage() throws UnsupportedEncodingException{
		StringBuffer sql = new StringBuffer();
		HttpServletRequest request = getRequest();
		request.setCharacterEncoding("utf-8");
		HttpServletResponse response = getResponse();
		response.setContentType("text/html;charset = utf-8");
		//显示的起始索引
		int limitStart = Integer.parseInt(getPara("start").toString());
		//显示的行数
		int limitLength = Integer.parseInt(getPara("length").toString());
		String draw = "0";
		draw = getPara("draw");
		permissonService = new PermissionServiceImpl();
		int totalData = permissonService.query_totalPermissonService(sql.toString());
		List<Permission> ps = permissonService.query_allPermissonByPageService(sql.toString(), limitStart, limitLength);
		Map<Object, Object> info = new HashMap<Object, Object>();
		info.put("data",ps);
		info.put("recordsTotal", totalData);
		info.put("recordsFiltered", totalData);
		info.put("draw", draw);
		renderJson(info);
	}
	public void add_permission(){
		permissonService = new PermissionServiceImpl();
		Permission permisson = getModel(Permission.class);
		boolean flag = permissonService.add_permissionService(permisson);
		String message = null;
		if(flag){
			message  = "添加成功";
			setAttr("message",message);
		}
		else{
			message = "添加失败";
			setAttr("message",message);
		}
		renderJsp("/WEB-INF/jsp/cue_message.jsp");
	}
	public void toAddPermissionView(){
		Permission permission = getModel(Permission.class);
		permissonService = new PermissionServiceImpl();
		List<Permission> ps = permissonService.query_permissionForTypeDao(permission);
		setAttr("ps", ps);
		renderJsp("/WEB-INF/jsp/add_permission.jsp");
	}
	public void toUpdatePermissionView(){
		Permission p = getModel(Permission.class);
		permissonService = new PermissionServiceImpl();
		List<Permission> ps = permissonService.query_permissionForTypeDao(null);
		setAttr("ps", ps);
		setAttr("permission", permissonService.query_permissionByIdService(p));
		renderJsp("/WEB-INF/jsp/update_permission.jsp");
	}
	public void update_permission(){
		Permission permission = getModel(Permission.class);
		permissonService = new PermissionServiceImpl();
		boolean flag = permissonService.update_permissionService(permission);
		String message = null;
		if(flag){
			message = "编辑成功";
			setAttr("message",message);
		}
		else{
			message = "编辑失败";
			setAttr("message",message);
		}
		renderJsp("/WEB-INF/jsp/cue_message.jsp");
	}
	public void delete_permissionById(){
		Permission permission = getModel(Permission.class);
		permissonService = new PermissionServiceImpl();
		boolean flag = permissonService.delete_permissionByIdService(permission);
		renderJson(flag);
	}
}
